package com.gedi;

import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

/**
 * @Author:RenPu
 * @Date: 2020/2/15 19:34
 * @Version: 1.0
 * @description:
 */
public class OrderReduce extends TableReducer<Text, DoubleWritable, NullWritable> {




    /**
     *
     * @param key userId
     * @param values 本年度的订单金额集合
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
        Double sum=0.0D;
        Iterator<DoubleWritable> iterator = values.iterator();
        if(iterator.hasNext()){
            sum+= iterator.next().get();
        }

        Put put = new Put((key.toString()+":2018").getBytes());

        put.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("total"),Bytes.toBytes(sum));

        context.write(null,put);


    }
}
